package com.xmtlxx.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.wms.domain.InventoryDtl;
import com.xmtlxx.wms.domain.bo.InventoryDtlBo;
import com.xmtlxx.wms.domain.vo.InventoryDtlVo;
import com.xmtlxx.wms.mapper.InventoryDtlMapper;
import com.xmtlxx.wms.service.IInventoryDtlService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 盘点明细Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class InventoryDtlServiceImpl implements IInventoryDtlService {

    private final InventoryDtlMapper baseMapper;

    /**
     * 查询盘点明细
     *
     * @param id 盘点明细主键
     * @return 盘点明细
     */
    @Override
    public InventoryDtlVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectInventoryDtlVoByID(id);
    }

    /**
     * 查询盘点明细列表
     *
     * @param bo 盘点明细
     * @return 盘点明细
     */
    @Override
    public TableDataInfo<InventoryDtlVo> queryPageList(InventoryDtlBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<InventoryDtl> lqw = buildQueryWrapper(bo);
       // Page<InventoryDtlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<InventoryDtlVo> result = baseMapper.selectPageInventoryDtlList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询盘点明细列表
     *
     * @param bo 盘点明细
     * @return 盘点明细
     */
    @Override
    public List<InventoryDtlVo> queryList(InventoryDtlBo bo) {
       // LambdaQueryWrapper<InventoryDtl> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectInventoryDtlList(bo);
    }

    private LambdaQueryWrapper<InventoryDtl> buildQueryWrapper(InventoryDtlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InventoryDtl> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getInventoryId()), InventoryDtl::getInventoryId, bo.getInventoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getStockId()), InventoryDtl::getStockId, bo.getStockId());
        lqw.eq(StringUtils.isNotBlank(bo.getPno()), InventoryDtl::getPno, bo.getPno());
        lqw.like(StringUtils.isNotBlank(bo.getName()), InventoryDtl::getName, bo.getName());
        lqw.eq(bo.getResults() != null, InventoryDtl::getResults, bo.getResults());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), InventoryDtl::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增盘点明细
     *
     * @param bo 盘点明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(InventoryDtlBo bo) {
        InventoryDtl add = BeanUtil.toBean(bo, InventoryDtl.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改盘点明细
     *
     * @param bo 盘点明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(InventoryDtlBo bo) {
        InventoryDtl update = BeanUtil.toBean(bo, InventoryDtl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(InventoryDtl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除盘点明细
     *
     * @param ids 需要删除的盘点明细主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
